#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int push_btn(vector<vector<int>> &grid, int i, int s)
{
	int bit = 0;
	for (auto v : grid[i]) {
		if (v == 1 && ((s >> bit) & 1) == 1) {
			s = s & ~(1 << bit);
		}
		if (v == -1 && ((s >> bit) & 1) == 0) {
			s = s | (1 << bit);
		}
		bit++;
	}
	return s;
}

int count(vector<vector<int>> &grid, int s)
{
	int n = grid.size();
	int m = grid[0].size();

	vector<int> vis(1 << m, 0);
	queue<int> q;
	int ans = 0;
	q.push(s);
	vis[s] = 1;
	while (!q.empty()) {
		int size = q.size();
		ans++;
		while (size--) {
			int curr = q.front();
			q.pop();
			for (int i = 0; i < n; i++) {
				int ns = push_btn(grid, i, curr);
				if (ns == 0) {
					return ans;
				}
				if (!vis[ns]) {
					vis[ns] = 1;
					q.push(ns);
				}
			}
		}
	}
	return -1;
}

int main()
{
	int n = 0, m = 0;
	cin >> n >> m;
	vector<vector<int>> grid(m, vector<int>(n, 0));
	for (int i = 0; i < m; i++) {
		for (int j = 0; j < n; j++) {
			cin >> grid[i][j];
		}
	}
	cout << count(grid, (1 << n) - 1) << endl;
	return 0;
}